//获取操作对象
var box1=document.querySelector(".panel-body")
var box2=document.querySelector(".panel-body2")
var box3=document.querySelector(".media-body")
var box4=document.querySelectorAll(".haha")



//获取地址栏后面的参数信息
var search=location.search
var o1
//判断地址栏中是否有参数
if(search){
    //获取参数信息
    var ar1=search.split('?')[1].split('=')
    //判断当前参数是否为id
    if(ar1[0]=="id"){
        show()
    }else{
        alert("参数有误")
        location.href='./index1.html'
    }
}else{
    alert("非法进入")
    location.href='./index1.html'
}
//创建显示数据的函数
async function show(){
    //获取数据库中的数据信息
    o1=await PromiseAjax({
        url:'../php/xiangqing.php',
        data:'id='+ar1[1],
        datatype:'json'
    })
  
    //渲染当前商品信息
//     <div class="btn-group" role="group" aria-label="...">
//     <button type="button" class="btn btn-default">XXL</button>
//     <button type="button" class="btn btn-default">XL</button>
//     <button type="button" class="btn btn-default">L</button>
//     <button type="button" class="btn btn-default">M</button>
//     <button type="button" class="btn btn-default">S</button>
// </div>
    var str=`
    <div class="media">
        <div class="media-body">
        <br/>
        <br/>
        <h3 class="media-heading" >${o1.cat_three_id}</h3>
        <br/>
        <span>商品价格</span>
        <h3 style="color:red">￥${o1.goods_price}</h3>
        <br/>
        <span>商品样式：</span>
        <button type="button" class="btn btn-default">${o1.cat_one_id}</button>
        <button type="button" class="btn btn-default">${o1.cat_two_id}</button>
        <button type="button" class="btn btn-default">${o1.cat_three_id}</button>
        <br/>
        <br/>
        <span _ngcontent-c7="" class="do-count-title">数量选择</span> 
        <br/>
        <div style="position: relative;">
        <span style="float: left;text-align: center;background: url(../image/download.png) no-repeat;
        display: inline-block;width: 36px;height: 36px;line-height: 40px;text-indent: -9999em;
        position: absolute;left: -9px;right: -4px;top: 0;height: 45px;content:" ";">-</span> 
        <span style="position: absolute;left: 45px;right: -4px;top:7px;height: 45px;content:" ";">1</span> 
        <span style="float: left;text-align: center;background: url(../image/download.png) no-repeat;
        display: inline-block;width: 36px;height: 36px;line-height: 40px;text-indent: -9999em;
        position: absolute;left: 64px;right: -4px;top: 0;height: 45px;content:" ";">+</span> 
        </div>
        <br/>
        <br/>
        <br/>
        <span>运费险</span>
        <button type="button" class="btn btn-default">买</button>
        <button type="button" class="btn btn-default">不买</button>
        <button type="button" class="btn btn-default">买一半</button>
        <br/>
        <br/>
        <br/>
        <br/>
        <a class="btn btn-success" href="./cart.html" role="button">立即购买</a>
        <a>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp<a/>
        <a class="btn btn-info" href="javascript:;" role="button">加入购物车</a>
        </div>
        <br/>
    </div>
    `
     //渲染当前商品详情信息
    var str2=`
    <div class="media2">
      ${o1.goods_introduce}
    </div>`
    var str3=`${o1.goods_big_logo}`
    
    box1.innerHTML=str
    box2.innerHTML=str2
    box4[0].src=str3
    box4[1].src=str3
    box4[2].src=str3
}

//给大盒子对象绑定点击事件
box1.onclick=function(e){
    //事件对象兼容
    var e = e || window.event
    //目标对象兼容
    var target=e.target || e.srcElement
    //判断当前点击的是否为"加入购物车"
    if(target.innerHTML=="加入购物车"){
        //获取localStorage中的cartlist1键所对应的数据
        var cartlist1=localStorage.getItem("cartlist1")||"[]"
        //把获取的字符串转为数组对象
        cartlist1=eval('('+cartlist1+')')
        //判断当前数组中是否有值
        if(cartlist1.length>0){
            var bool=false //表示当前添加的商品是否在locaStrong存放过
            //遍历数组对象
            for(var i=0;i<cartlist1.length;i++){
                //判断当前遍历出来的商品id是否等于当前添加的商品id
                if(cartlist1[i].goods_id==o1.goods_id){
                    bool=true
                    //判断当前商品的购买量是否大于等于库存量
                    if(cartlist1[i].cart_number<cartlist1[i].goods_number){
                        //直接在当前商品的基础上添加数量
                        cartlist1[i].cart_number++
                        //把cartlist1存放在localStorage
                        localStorage.setItem("cartlist1",JSON.stringify(cartlist1))
                    }
                }
            }
            if(!bool){
                //修改当前商品的数量
                o1.cart_number=1
                //直接把当前商品追加到数组中
                cartlist1.push(o1)
                //把cartlist1存放在localStorage
                localStorage.setItem("cartlist1",JSON.stringify(cartlist1))
            }

        }else{
            //修改当前商品的数量
            o1.cart_number=1
            //直接把当前商品追加到数组中
            cartlist1.push(o1)
            //把cartlist1存放在localStorage
            localStorage.setItem("cartlist1",JSON.stringify(cartlist1))
        }
    }
}



   //获取操作对象
